package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("employee")
@Slf4j
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("login")
    public R<Employee> log(@RequestBody Employee employee, HttpServletRequest request){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        String username = employee.getUsername();
        String password = employee.getPassword();
        //判断数据库中是否存在用户名
        wrapper.eq(Employee::getUsername,username);
        //根据用户名获取用户
        Employee emp = employeeService.getOne(wrapper);
        if (emp == null) {
            return R.error("用户名错误");
        }
        //password编码后对比密码是否一致
       String password2= DigestUtils.md5DigestAsHex(password.getBytes());
        if (!StringUtils.equals(emp.getPassword(),password2)) {
            return R.error("密码错误");
        }
        //判断状态
        if (emp.getStatus()==0) {
            return R.error("员工已被禁用");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logOut(HttpServletRequest request){
        //invalide :销毁session
        request.getSession().invalidate();
        return R.success("退出成功！");
    }

    /**
     * 添加员工
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> saveOut(@RequestBody Employee employee){
        //密码加密添加到数据库
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 员工分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        //分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //lambda条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getUsername,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询条件
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        //通过得到的id获取对象将数据回显，再用上面的修改方法
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
